package com.educhains.module.chat.service.impl;

import com.educhains.module.chat.persist.mybatis.MessageMapper;
import com.educhains.module.chat.service.MessageService;
import com.educhains.module.chat.vo.Message;
import com.educhains.module.user.service.UserService;
import com.educhains.web.util.HttpUtils;
import com.educhains.web.util.PropertyUtils;
import com.educhains.web.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private UserService userService = null;

    @Autowired
    private MessageMapper messageMapper = null;

    public int addMessage(Message message) {
        message.setMessageId(StringUtils.generateRandomString(32));
        message.setMessageToName(userService.selectUserById(message.getMessageToId()).getUserName());
        return messageMapper.insert(message);
    }

    public List<Message> getChatLog(String messageFromId, String messageToId) {
        return messageMapper.getChatLog(messageFromId, messageToId);
    }

    public String getAIChat(String userId, String text) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("id", userId);
        paramMap.put("text", text);
        String url = PropertyUtils.getProperty("ai.url");
        return HttpUtils.sendPost(url, paramMap);
    }
}
